<?php
    include_once('../../src/classes/class.mysql.php');
   
    session_start();
    
    $comtec_id_mainteiner = $_GET["id"];
    
    $db = new MySQL();
    $tsql = "SELECT comtec_id_responsible,CONCAT_WS(' ',name,lastname1,lastname2) as encargado,position,rut,DATE_FORMAT(company_income_date,'%d/%m/%Y') as fecha,cod_mainteiner 
             FROM responsible WHERE comtec_id_mainteiner=$comtec_id_mainteiner and status_reg=1 ORDER BY lastname1 ASC,lastname2 ASC,name ASC ";
    $stmt = $db->query($tsql);       
    
    $texto  = "<table class='table'>";
    if($_SESSION["users"]=="managers"){         
     $texto .= "<tr><th colspan=2></th>";
    }elseif ($_SESSION["users"]!="clients" && $_SESSION["users"]!="technicians") {
     $texto .= "<tr><th></th>";  
    }          
    $texto .= "<th>Codigo</th>
              <th>Nombre</th>
              <th>Cargo</th>
              <th>RUT</th>
              <th>Fecha de Ingreso</th>
              </tr>";
    $cont = 0;
    while ($row = mysql_fetch_array($stmt, MYSQL_ASSOC)){
     $cont += 1;
     $encargado=$row['encargado'];   
     $comtec_id_responsible=$row['comtec_id_responsible'];   
     $cod_mainteiner=$row['cod_mainteiner'];
     $position=$row['position'];
     $rut=$row['rut'];
     $rut_cod = substr($rut, -1, 1);
     $rut = substr($rut,0,strlen($rut)-1);
     $income_date=$row['fecha'];
     $texto .= "<tr>";
     if($_SESSION["users"]!="clients" && $_SESSION["users"]!="technicians"){
      $url_edit='http://'.$_SERVER['HTTP_HOST'].'/iconos/edit.png';
      $texto .= "<td><a href='javascript:loadResponsible(".$comtec_id_responsible.");'><img src='$url_edit' alt='editar' style='border: 0px;' /></a></td>";
     }
     if($_SESSION["users"]=="managers"){
      $url_delete='http://'.$_SERVER['HTTP_HOST'].'/iconos/delete.png';
      $texto .= "<td><a href='javascript:deleteResponsible(".$comtec_id_responsible.");'><img src='$url_delete' alt='eliminar' style='border: 0px;' /></a></td>";
     }
     $texto .= "<td><a href='javascript:responsible(".$comtec_id_responsible.");'>$cod_mainteiner</a></td>";
     $texto .= "<td>$encargado</td>";
     $texto .= "<td>$position</td>";
     $texto .= "<td>".$rut."-".$rut_cod."</td>";
     $texto .= "<td>$income_date</td>";
     $texto .= "</tr>";
    }
    $texto .= "</table>";
    $texto .= '<div id="result"></div>';
    echo "<p class='total'>Total: $cont</p>".$texto;
?>
